<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="google-site-verification" content="XG_J8ModD9lm1c0KqNVAQ3XvUfIQFhmYZgdxyeUFxVI" />







  <meta name="baidu-site-verification" content="BYxasfLxtf" />







  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, Kieran" />





  <link rel="alternate" href="/atom.xml" title="Kieran" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta property="og:type" content="website">
<meta property="og:title" content="Kieran">
<meta property="og:url" content="http://kieran.cn/index.html">
<meta property="og:site_name" content="Kieran">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Kieran">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://kieran.cn/"/>





  <title>  Kieran  </title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
   page-home 
 ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Kieran</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://kieran.cn/2017/06/26/IOS-Clang-Attributes/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kieran zhi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kieran">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/06/26/IOS-Clang-Attributes/" itemprop="url">IOS Clang Attributes 属性</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-26T09:53:00+08:00">
                2017-06-26
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/IOS/" itemprop="url" rel="index">
                    <span itemprop="name">IOS</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          
          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><code>__attribute__</code> 是一个编译器指令，规定了声明的特性，这就有更多的错误检查和先进的优化的方法。开发者可以向编译器表达某种要求，比如 静态分析器，名字重名，代码生成等过程，一般格式 <code>__attribute__(xxx)</code>格式，常用的属性Cocoa 定义成了宏，比如 NS_CLASS_AVAILABLE_IOS(9_0) 通过 `<strong>attribute</strong>(warn_unused_result(…)定义的，用到的属性是 warn_unused_result，下面是集中常用的属性：</p>
<h3 id="objc-subclassing-restricted"><a href="#objc-subclassing-restricted" class="headerlink" title="objc_subclassing_restricted"></a>objc_subclassing_restricted</h3><p>这个属性可以定义一个太监类，一个不可以被继承的类</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">__attribute__((objc_subclassing_restricted))</div><div class="line">@interface Eunuch : NSObject</div><div class="line">@end</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">@interface Child : Eunuch // &lt;--- 编译会报错,Cannot subclass a class with objc_subclassing_restricted attribute</div><div class="line">@end</div></pre></td></tr></table></figure>
<h3 id="warn-unused-result-constructor"><a href="#warn-unused-result-constructor" class="headerlink" title="warn_unused_result constructor"></a>warn_unused_result constructor</h3><p>这个属性声明 子类调用这个方法必须调用super，否则给出编译警告，使用方法如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">@interface TestObject : NSObject</div><div class="line">- (void)testMethod __attribute__((objc_requires_super));</div><div class="line">@end</div><div class="line"></div><div class="line">@implementation TestObject</div><div class="line">- (void)testMethod &#123;</div><div class="line">    NSLog(@&quot;hail testMethod!&quot;);</div><div class="line">&#125;</div><div class="line">    </div><div class="line">@end</div><div class="line"></div><div class="line">@interface ChildObject : TestObject</div><div class="line">@end</div><div class="line">@implementation ChildObject</div><div class="line"></div><div class="line">- (void)testMethod &#123;</div><div class="line">&#125; // &lt;--- 编译会警告，Method possibly missing a [super testMethod] call</div><div class="line"></div><div class="line">@end</div></pre></td></tr></table></figure>
<h3 id="objc-boxable"><a href="#objc-boxable" class="headerlink" title="objc_boxable"></a>objc_boxable</h3><p>这个属性可以将  struct 或者union 转换成 NSValue</p>
<blockquote>
<p>Objective-C 可以使用 @(…)<code>@(1.1)</code> 转换成 NSNumber 对象，如果想让 struct 或者union 转换成 NSValue，可以使用这个属性</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">typedef struct __attribute__((objc_boxable)) &#123;</div><div class="line">    CGFloat x, y, width, height;</div><div class="line">&#125; XXRect;</div><div class="line"></div><div class="line">- (void)testmethod&#123;</div><div class="line">    CGRect rect1 = &#123;1.0,1.0,1.0,1.0&#125;;</div><div class="line">    XXRect rect2 = &#123;1.0,1.0,1.0,1.0&#125;;</div><div class="line"></div><div class="line">    NSValue *value1 = @(rect1); // &lt;--- 编译报错 ，Illegal type &apos;CGRect&apos; (aka &apos;struct CGRect&apos;) used in a boxed expression</div><div class="line">    NSValue *value2 = @(rect2); </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="constructor-destructor"><a href="#constructor-destructor" class="headerlink" title="constructor/destructor"></a>constructor/destructor</h3><p>构造器和解析器，加上这两个属性的函数会在加载可以执行文件（或者shared library） load 和 unload 时候调用，可以理解为 在main() 函数开始前调用，后return 后调用。也可以理解为constructor程序启动的时候调用，destructor 在程序杀死或退出时候调用。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">int main(int argc, char * argv[]) &#123;</div><div class="line">    @autoreleasepool &#123;</div><div class="line">        NSLog(@&quot;main&quot;);</div><div class="line">        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">__attribute__((constructor))</div><div class="line">static void beforeMain() &#123;</div><div class="line">    </div><div class="line">    NSLog(@&quot;beforeMain&quot;);</div><div class="line">&#125;</div><div class="line">    </div><div class="line">__attribute__((destructor))</div><div class="line">static void afterMain() &#123;</div><div class="line">    NSLog(@&quot;afterMain&quot;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<blockquote>
<p>+load 方法回比constructor更加早执行，因为dyld（动态库）加载image（mach-o）会通知runtime 去加载其中所有的类，每个类加载的时候，所对应的load 方法随之调用，全部加载完成后，dyld 才会调用这个 image 中所有的 constructor 方法。若有多个 constructor 且想控制优先级的话，可以写成 <code>__attribute__((constructor(101)))</code>，里面的数字越小优先级越高，1 ~ 100 为系统保留。</p>
</blockquote>
<p>所以 constructor 是一个干坏事的绝佳时机：</p>
<ol>
<li>所有 Class 都已经加载完成</li>
<li>main 函数还未执行</li>
<li>无需像 +load 还得挂载在一个 Class 中<br>FDStackView 的 FDStackViewPatchEntry 方法便是使用的这个时机来实现偷天换日的伎俩。</li>
</ol>
<h3 id="enable-if"><a href="#enable-if" class="headerlink" title="enable_if"></a>enable_if</h3><p>这个属性只能用在 C 函数上，可以用来实现参数的静态检查：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">static void printValidAge(int age)</div><div class="line">__attribute__((enable_if(age &gt; 0 &amp;&amp; age &lt; 120, &quot;你丫火星人？&quot;))) &#123;</div><div class="line">    printf(&quot;%d&quot;, age);</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>它表示调用这个函数时必须满足 age &gt; 0 &amp;&amp; age &lt; 120 才被允许，于是乎：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">printValidAge(26); // √</div><div class="line">printValidAge(150); // &lt;--- Compile Error</div><div class="line">printValidAge(-1); // &lt;--- Compile Error</div></pre></td></tr></table></figure>
<h3 id="cleanup"><a href="#cleanup" class="headerlink" title="cleanup"></a>cleanup</h3><p>用于修饰一个变量，在它的作用域结束时可以自动执行一个指定的方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">// 指定一个cleanup方法，注意入参是所修饰变量的地址，类型要一样</div><div class="line">// 对于指向objc对象的指针(id *)，如果不强制声明__strong默认是__autoreleasing，造成类型不匹配</div><div class="line">static void stringCleanUp(__strong NSString **string) &#123;</div><div class="line">    NSLog(@&quot;%@&quot;, *string);</div><div class="line">&#125;</div><div class="line">// 在某个方法中：</div><div class="line">- (void)test&#123;</div><div class="line">    __strong NSString *string __attribute__((cleanup(stringCleanUp))) = @&quot;sunnyxx&quot;;</div><div class="line">&#125; // 当运行到string作用域结束时，自动调用stringCleanUp</div></pre></td></tr></table></figure>
<p>作用域结束，包括大括号结束、return、goto、break、exception等各种情况。<br>当然，可以修饰的变量不止NSString，自定义Class或基本类型都是可以的：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">// 自定义的Class</div><div class="line">static void sarkCleanUp(__strong Sark **sark) &#123;</div><div class="line">    NSLog(@&quot;%@&quot;, *sark);</div><div class="line">&#125;</div><div class="line">__strong Sark *sark __attribute__((cleanup(sarkCleanUp))) = [Sark new];</div><div class="line">// 基本类型</div><div class="line">static void intCleanUp(NSInteger *integer) &#123;</div><div class="line">    NSLog(@&quot;%d&quot;, *integer);</div><div class="line">&#125;</div><div class="line">NSInteger integer __attribute__((cleanup(intCleanUp))) = 1;</div></pre></td></tr></table></figure>
<blockquote>
<p>一个作用域内有若干个cleanup的变量，他们的调用顺序是先入后出的栈式顺序； 而且，cleanup是先于这个对象的dealloc调用的。</p>
</blockquote>
<p>用于修饰一个block<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">static void blockCleanUp(__strong void(^*block)(void)) &#123;</div><div class="line">    (*block)();</div><div class="line">&#125;</div><div class="line">#define onExit\</div><div class="line">    __strong void(^block)(void) __attribute__((cleanup(blockCleanUp), unused)) = ^</div><div class="line">    </div><div class="line">    </div><div class="line">- (void)testOnExit&#123;</div><div class="line">    NSLock *aLock = [[NSLock alloc] init];</div><div class="line">    [aLock lock];</div><div class="line">    onExit&#123;</div><div class="line">        [lock unlock];</div><div class="line">        NSLog(@&quot;方法已经执行完成&quot;);</div><div class="line">    &#125;;</div><div class="line">    //</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="overloadable"><a href="#overloadable" class="headerlink" title="overloadable"></a>overloadable</h3><p>用于 C 函数，可以定义若干个函数名相同，但参数不同的方法，调用时编译器会自动根据参数选择函数原型：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">__attribute__((overloadable)) void logAnything(id obj) &#123;</div><div class="line">    NSLog(@&quot;%@&quot;, obj);</div><div class="line">&#125;</div><div class="line">__attribute__((overloadable)) void logAnything(int number) &#123;</div><div class="line">    NSLog(@&quot;%@&quot;, @(number));</div><div class="line">&#125;</div><div class="line">__attribute__((overloadable)) void logAnything(CGRect rect) &#123;</div><div class="line">    NSLog(@&quot;%@&quot;, NSStringFromCGRect(rect));</div><div class="line">&#125;</div><div class="line">// Tests</div><div class="line">logAnything(@[@&quot;1&quot;, @&quot;2&quot;]);</div><div class="line">logAnything(233);</div><div class="line">logAnything(CGRectMake(1, 2, 3, 4));</div></pre></td></tr></table></figure>
<h3 id="objc-runtime-name"><a href="#objc-runtime-name" class="headerlink" title="objc_runtime_name"></a>objc_runtime_name</h3><p>可以吧 <code>@interface</code> 或 <code>@protocol</code>，将类或协议的名字在编译时指定成另一个：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">__attribute__((objc_runtime_name(&quot;SarkGay&quot;)))</div><div class="line">@interface Sark : NSObject</div><div class="line">@end</div><div class="line"></div><div class="line">NSLog(@&quot;%@&quot;, NSStringFromClass([Sark class])); // &quot;SarkGay&quot;</div></pre></td></tr></table></figure></p>
<blockquote>
<p>直接使用这个类名的地方都会被替换（唯一要注意的是这时用反射就不对了），最简单粗暴的用处就是去做个类名混淆：</p>
</blockquote>
<h3 id="format"><a href="#format" class="headerlink" title="format"></a>format</h3><p>格式化字符串<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">//意思是第二个参数my_format参数是二个格式化字符串，从第三个参数开始检查</div><div class="line">extern int my_printf (void *my_object, const char *my_format, ...) __attribute__((format(printf, 2, 3)))</div></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">意思是第二个参数my_format参数是一个格式化字符串，从第二个参数开始检查</div><div class="line">FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) NS_NO_TAIL_CALL;</div></pre></td></tr></table></figure>
<h3 id="deprecated"><a href="#deprecated" class="headerlink" title="deprecated"></a>deprecated</h3><p>定义被弃用的方法或属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">//标识改方法已经弃用</div><div class="line">- (void)preMethod:(NSString *)string __attribute__((deprecated(&quot;preMethod已经被弃用，请使用newMethod&quot;)));</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">@property(nonatomic,strong) NSString * variable __attribute__((deprecated(&quot;preMethod已经被弃用，请使用newMethod&quot;)));</div></pre></td></tr></table></figure>
<h3 id="warn-unused-result"><a href="#warn-unused-result" class="headerlink" title="warn_unused_result"></a>warn_unused_result</h3><p><code>attribute((availability(os,introduced=m,deprecated=n, obsoleted=o,message=&quot;&quot; VA_ARGS)))</code> 指明使用版本范围</p>
<p>os 指系统的版本，m 指明引入的版本，n 指明过时的版本，o 指完全不用的版本，message 可以写入些描述信息</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">- (void)method __attribute__((availability(ios,introduced=3_0,deprecated=6_0,obsoleted=7_0,message=&quot;iOS3到iOS7版本可用，iOS7不能用&quot;)));</div></pre></td></tr></table></figure>
<h3 id="unavailable"><a href="#unavailable" class="headerlink" title="unavailable"></a>unavailable</h3><p>这个会在编译过程中告知方法不可用，如果使用了还会让编译失败。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">- (void)method __attribute__((unavailable(&quot;该方法不可用&quot;)))</div></pre></td></tr></table></figure>
<h3 id="unused"><a href="#unused" class="headerlink" title="unused"></a>unused</h3><p>没有使用也不报警告</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">NSString * variable __attribute__((unused));</div><div class="line">//没有调用也不会报警告    </div><div class="line">- (void)testmethod __attribute__((unused));</div></pre></td></tr></table></figure>
<h3 id="objc-designated-initializer"><a href="#objc-designated-initializer" class="headerlink" title="objc_designated_initializer"></a>objc_designated_initializer</h3><p>用于声明初始化方法，必须以<code>- (id)initTXXX</code> 方法开头。</p>
<ul>
<li><p>如果是 objc_designated_initializer 初始化的方法必须调用覆盖实现 super 的 objc_designated_initializer 方法。</p>
</li>
<li><p>如果不是 objc_designated_initializer 的初始化方法，但是该类有 objc_designated_initializer 的初始化方法，那么必须调用该类的 objc_designated_initializer 方法或者非 objc_designated_initializer 方法，而不能够调用 super 的任何初始化方法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">- (id)initMethod __attribute__((objc_designated_initializer));</div></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="const"><a href="#const" class="headerlink" title="const"></a>const</h3><p>用于数值类型参数的函数，多次调用相同的数值型参数，返回是相同的，只在第一次是需要进行运算，后面只返回第一次的结果，这时编译器的一种优化处理方式。</p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://kieran.cn/2017/06/24/Git-多SSH-Key/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kieran zhi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kieran">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/06/24/Git-多SSH-Key/" itemprop="url">AutomatorX 使用</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-24T17:23:00+08:00">
                2017-06-24
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/IOS/" itemprop="url" rel="index">
                    <span itemprop="name">IOS</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          
          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="AutomatorX"><a href="#AutomatorX" class="headerlink" title="AutomatorX"></a>AutomatorX</h1><h3 id="WebDriverAgent-使用"><a href="#WebDriverAgent-使用" class="headerlink" title="WebDriverAgent 使用"></a>WebDriverAgent 使用</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">// 下载 WebDriverAgent</div><div class="line">git clone https://github.com/facebook/WebDriverAgent&gt;</div><div class="line"></div><div class="line">// 配置 WebDriverAgent</div><div class="line">./Scripts/bootstrap.sh</div><div class="line">//该脚本会使用Carthage下载所有的依赖，使用npm打包响应的js文件</div><div class="line"></div><div class="line">/* IPhone 真机调试 usb 端口不能交互*/</div><div class="line">brew install imobiledevice</div><div class="line">iproxy 8100 8100</div></pre></td></tr></table></figure>
<blockquote>
<p>使用 <a href="http://localhost:8100/status" target="_blank" rel="external">http://localhost:8100/status</a> 可以查看连接信息</p>
<p>界面信息 <a href="http://localhost:8100/inspector" target="_blank" rel="external">http://localhost:8100/inspector</a></p>
</blockquote>
<h4 id="使用终端代替xcode"><a href="#使用终端代替xcode" class="headerlink" title="使用终端代替xcode"></a>使用终端代替xcode</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"># 解锁keychain，以便可以正常的签名应用，</div><div class="line">PASSWORD=&quot;replace-with-your-password&quot;</div><div class="line">security unlock-keychain -p $PASSWORD ~/Library/Keychains/login.keychain</div><div class="line"></div><div class="line"># 获取设备的UDID</div><div class="line">UDID=$(idevice_id -l | head -n1)</div><div class="line"></div><div class="line"># 运行测试</div><div class="line">xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination &quot;$UDID&quot; test</div></pre></td></tr></table></figure>
<h3 id="安装-OpenCV"><a href="#安装-OpenCV" class="headerlink" title="安装 OpenCV"></a>安装 OpenCV</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"># add opencv</div><div class="line">brew tap homebrew/science</div><div class="line"></div><div class="line"># install opencv</div><div class="line">brew install opencv</div><div class="line"></div><div class="line">//安装必要的python库</div><div class="line"></div><div class="line">pip install numpy</div><div class="line">pip install matplotlib</div></pre></td></tr></table></figure>
<h3 id="安装-AutomatorX"><a href="#安装-AutomatorX" class="headerlink" title="安装 AutomatorX"></a>安装 AutomatorX</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"># virtualenv venv &amp;&amp; . venv/bin/activate</div><div class="line">pip install --upgrade --pre atx</div><div class="line">pip install opencv_contrib_python</div></pre></td></tr></table></figure>
<p>装完之后用命令行简单测试下安装是否成功</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">python -m atx version 查看atx版本号</div><div class="line">python -m atx doctor 检查环境配置是否正常</div></pre></td></tr></table></figure>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://kieran.cn/2017/06/22/Xcode-git-忽略无关文件/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kieran zhi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kieran">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/06/22/Xcode-git-忽略无关文件/" itemprop="url">Xcode git 忽略无关文件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-22T17:37:00+08:00">
                2017-06-22
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Git/" itemprop="url" rel="index">
                    <span itemprop="name">Git</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          
          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p> Git<strong>版本控制</strong>的时候，有很多非代码文件也会被跟踪，比较常见的如一些影藏文件DS_Store、以xcworkspace、xcuserstate、xcuserdata等结尾的状态文件等。为了不把这些文件加入版本控制体系中，我们需要进行以下三步设置，注意，每一步必不可少，也不可交换位置，下文会具体解释原因。 </p>
<ol>
<li>第一步：进入git的代码仓库，执行以下的代码</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">git rm --cached *.xcuserstate</div><div class="line">git rm --cached *.xcuserdata</div></pre></td></tr></table></figure>
<blockquote>
<p>这两行代码（或者可以有更多，自己修改后缀名即可，这里列出了常见的两种隐藏文件）表示不再追踪以这些后缀结尾的文件，注意这里的文件在执行代码前其实已经被追踪（Tracked），执行完后，将不再被追踪。</p>
</blockquote>
<ol>
<li>修改.gitignore文件</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim .gitignore</div></pre></td></tr></table></figure>
<blockquote>
<p>这时候会进入gitignore的文件的编辑界面，如果这个文件已经存在，则可以通过普通的文本编辑器直接进行修改（需要设置显示隐藏文件），复制以下内容进入.gitignore文件。（ignore.io推荐）</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">build/</div><div class="line">*.pbxuser</div><div class="line">!default.pbxuser</div><div class="line">*.mode1v3</div><div class="line">!default.mode1v3</div><div class="line">*.mode2v3</div><div class="line">!default.mode2v3</div><div class="line">*.perspectivev3</div><div class="line">!default.perspectivev3</div><div class="line">xcuserdata</div><div class="line">*.xccheckout</div><div class="line">*.moved-aside</div><div class="line">DerivedData</div><div class="line">*.xcuserstate</div></pre></td></tr></table></figure>
<blockquote>
<p>按esc键退出编辑，输入：wq保存文件。<br>注意这里的gitignore文件的修改，仅对未追踪（Untracked）的文件生效，所以首先要执行第一步，取消对以上类型文件的追踪。</p>
</blockquote>
<ol>
<li>第三步：提交此处版本修改<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">git add .</div><div class="line">git commit -m &quot;igonre files &quot;</div><div class="line">git push origin master</div></pre></td></tr></table></figure>
</li>
</ol>
<p><strong>重启Xcode并且尝试修改一个文件后执行commit，发现那些与代码无关的文件并不会被自动提交了</strong></p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://kieran.cn/2017/06/22/Android-四大组件之SharedPrefernces/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kieran zhi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kieran">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/06/22/Android-四大组件之SharedPrefernces/" itemprop="url">android:数据存储SharedPrefernces</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-22T16:51:00+08:00">
                2017-06-22
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/android/" itemprop="url" rel="index">
                    <span itemprop="name">android</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          
          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="Android-四大组件之使用SharedPrefernces"><a href="#Android-四大组件之使用SharedPrefernces" class="headerlink" title="Android 四大组件之使用SharedPrefernces"></a>Android 四大组件之使用SharedPrefernces</h1><h2 id="SharedPreferences到底是什么"><a href="#SharedPreferences到底是什么" class="headerlink" title="SharedPreferences到底是什么"></a>SharedPreferences到底是什么</h2><p>它是一个轻量级的存储类，特别适合用于保存软件配置参数。使用SharedPreferences保存数据，其背后是用xml文件存放数据，文件存放在/data/data/packagename/shared_prefs目录下：</p>
<h2 id="SharedPreferences使用"><a href="#SharedPreferences使用" class="headerlink" title="SharedPreferences使用"></a>SharedPreferences使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">SharedPreferences sharedPreferences = getSharedPreferences(&quot;testshare&quot;, Context.MODE_PRIVATE);</div><div class="line">Editor editor = sharedPreferences.edit();//获取编辑器</div><div class="line">editor.putString(&quot;name&quot;, &quot;kieran&quot;);</div><div class="line">editor.putBoolean(&quot;isboy&quot;, true);</div><div class="line">editor.commit();//提交修改</div></pre></td></tr></table></figure>
<p>生成的testshare.xml文件内容如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&apos;1.0&apos; encoding=&apos;utf-8&apos; standalone=&apos;yes&apos; ?&gt;</div><div class="line">&lt;map&gt;</div><div class="line">&lt;string name=&quot;name&quot;&gt;kieran&lt;/string&gt;</div><div class="line">&lt;boolean name=&quot;sex&quot;&gt;true&lt;/boolean&gt;</div><div class="line">&lt;/map&gt;</div></pre></td></tr></table></figure>
<p>生成的文件路径</p>
<p><code>/data/data/packagename/shared_prefs/testshare.xml</code></p>
<h2 id="SharedPreferences获取"><a href="#SharedPreferences获取" class="headerlink" title="SharedPreferences获取"></a>SharedPreferences获取</h2><h3 id="一、-getSharedPreferences-String-name-int-mode"><a href="#一、-getSharedPreferences-String-name-int-mode" class="headerlink" title="一、 getSharedPreferences(String name, int mode)"></a>一、 getSharedPreferences(String name, int mode)</h3><pre><code>abstract SharedPreferences    getSharedPreferences(String name, int mode)
</code></pre><ol>
<li>name为本组件的配置文件名( 自己定义，也就是一个文件名，不需要带后缀名，系统自己添加)，当这个文件不存在时，直接创建，如果已经存在，则直接使用，</li>
<li>mode为操作模式，默认的模式为0或MODE_PRIVATE，还可以使用MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE<blockquote>
<p>mode指定为MODE_PRIVATE，则该配置文件只能被自己的应用程序访问。<br>mode指定为MODE_WORLD_READABLE，则该配置文件除了自己访问外还可以被其它应该程序读取。<br>mode指定为MODE_WORLD_WRITEABLE，则该配置文件除了自己访问外还可以被其它应该程序读取和写入</p>
</blockquote>
</li>
</ol>
<h3 id="二、PreferenceManager的方法getSharedPreferences"><a href="#二、PreferenceManager的方法getSharedPreferences" class="headerlink" title="二、PreferenceManager的方法getSharedPreferences()"></a>二、PreferenceManager的方法getSharedPreferences()</h3><ul>
<li>可以通过查看其源码</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">/** </div><div class="line"> * Gets a SharedPreferences instance that preferences managed by this will </div><div class="line"> * use. </div><div class="line"> *  </div><div class="line"> * @return A SharedPreferences instance pointing to the file that contains </div><div class="line"> *         the values of preferences that are managed by this. </div><div class="line"> */  </div><div class="line">public SharedPreferences getSharedPreferences() &#123;  </div><div class="line">    if (mSharedPreferences == null) &#123;  </div><div class="line">           mSharedPreferences = mContext.getSharedPreferences(mSharedPreferencesName,  </div><div class="line">                   mSharedPreferencesMode);  </div><div class="line">   &#125;  </div><div class="line">       return mSharedPreferences;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * Gets a SharedPreferences instance that preferences managed by this will</div><div class="line"> * use.</div><div class="line"> * @return A SharedPreferences instance pointing to the file that contains</div><div class="line"> *         the values of preferences that are managed by this.</div><div class="line"> */</div><div class="line">public SharedPreferences getSharedPreferences() &#123;</div><div class="line">    if (mSharedPreferences == null) &#123;</div><div class="line">        mSharedPreferences = mContext.getSharedPreferences(mSharedPreferencesName,</div><div class="line">                    mSharedPreferencesMode);</div><div class="line">    &#125;</div><div class="line">    return mSharedPreferences;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>其构造方法</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * This constructor should ONLY be used when getting default values from</div><div class="line"> * an XML preference hierarchy.</div><div class="line"> * &lt;p&gt;</div><div class="line"> * The &#123;@link PreferenceManager#PreferenceManager(Activity)&#125;</div><div class="line"> * should be used ANY time a preference will be displayed, since some preference</div><div class="line"> * types need an Activity for managed queries.</div><div class="line"> */</div><div class="line">private PreferenceManager(Context context) &#123;</div><div class="line">    init(context);</div><div class="line">&#125;</div><div class="line"></div><div class="line">private void init(Context context) &#123;</div><div class="line">    mContext = context;</div><div class="line">    setSharedPreferencesName(getDefaultSharedPreferencesName(context));</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> * Sets the name of the SharedPreferences file that preferences managed by this</div><div class="line"> * will use.</div><div class="line"> * </div><div class="line"> * @param sharedPreferencesName The name of the SharedPreferences file.</div><div class="line"> * @see Context#getSharedPreferences(String, int)</div><div class="line"> */</div><div class="line">public void setSharedPreferencesName(String sharedPreferencesName) &#123;</div><div class="line">    mSharedPreferencesName = sharedPreferencesName;</div><div class="line">    mSharedPreferences = null;</div><div class="line">&#125;</div><div class="line"></div><div class="line">private static String getDefaultSharedPreferencesName(Context context) &#123;</div><div class="line">    return context.getPackageName() + &quot;_preferences&quot;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>由以上方法，我们可以知道，最终我们调用getSharedPreferences()方法得到的是一个名为”yourpackageName_preferences“的偏好。同时其mode为默认私有。</p>
<h3 id="三、getDefaultSharedPreferences方法"><a href="#三、getDefaultSharedPreferences方法" class="headerlink" title="三、getDefaultSharedPreferences方法"></a>三、getDefaultSharedPreferences方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * Gets a SharedPreferences instance that points to the default file that is</div><div class="line"> * used by the preference framework in the given context.</div><div class="line"> * </div><div class="line"> * @param context The context of the preferences whose values are wanted.</div><div class="line"> * @return A SharedPreferences instance that can be used to retrieve and</div><div class="line"> *         listen to values of the preferences.</div><div class="line"> */</div><div class="line">public static SharedPreferences getDefaultSharedPreferences(Context context) &#123;</div><div class="line">    return context.getSharedPreferences(getDefaultSharedPreferencesName(context),</div><div class="line">            getDefaultSharedPreferencesMode());</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * Returns the name used for storing default shared preferences.</div><div class="line"> *</div><div class="line"> * @see #getDefaultSharedPreferences(Context)</div><div class="line"> * @see Context#getSharedPreferencesPath(String)</div><div class="line"> */</div><div class="line">public static String getDefaultSharedPreferencesName(Context context) &#123;</div><div class="line">    return context.getPackageName() + &quot;_preferences&quot;;</div><div class="line">&#125;</div><div class="line"></div><div class="line">private static int getDefaultSharedPreferencesMode() &#123;</div><div class="line">    return Context.MODE_PRIVATE;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这个方法是静态的，因此可以直接调用，同时它与我们调用getSharedPreferences()方法得到的返回值是一样的，只是调用的方式不同罢了。</p>
<h3 id="四、-getPreferences-mode-方法"><a href="#四、-getPreferences-mode-方法" class="headerlink" title="四、 getPreferences(mode) 方法"></a>四、 getPreferences(mode) 方法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * Retrieve a &#123;@link SharedPreferences&#125; object for accessing preferences</div><div class="line"> * that are private to this activity.  This simply calls the underlying</div><div class="line"> * &#123;@link #getSharedPreferences(String, int)&#125; method by passing in this activity&apos;s</div><div class="line"> * class name as the preferences name.</div><div class="line"> *</div><div class="line"> * @param mode Operating mode.  Use &#123;@link #MODE_PRIVATE&#125; for the default</div><div class="line"> *             operation.</div><div class="line"> *</div><div class="line"> * @return Returns the single SharedPreferences instance that can be used</div><div class="line"> *         to retrieve and modify the preference values.</div><div class="line"> */</div><div class="line">public SharedPreferences getPreferences(int mode) &#123;</div><div class="line">    return getSharedPreferences(getLocalClassName(), mode);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这个方法默认使用当前类类名作为文件的名称(不含包名)</p>
<h2 id="SharedPreferences-总结"><a href="#SharedPreferences-总结" class="headerlink" title="SharedPreferences 总结"></a>SharedPreferences 总结</h2><blockquote>
<p>SharedPreferences 存取数据使用的是xml 文件，系统对xml 文件进行了加载优化，当然也可以使用自定义其他方式的文件存取。</p>
</blockquote>
<p>如果想让其他应用能够读写，可以指定Modle权限。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">MODE_WORLD_READABLE</div><div class="line">MODE_WORLD_WRITEABLE</div></pre></td></tr></table></figure></p>
<h4 id="访问其他应用中的Preference（使用的应用-preference创建时指定了Context-MODE-WORLD-READABLE或者Context-MODE-WORLD-WRITEABLE-）"><a href="#访问其他应用中的Preference（使用的应用-preference创建时指定了Context-MODE-WORLD-READABLE或者Context-MODE-WORLD-WRITEABLE-）" class="headerlink" title="访问其他应用中的Preference（使用的应用 preference创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE ）"></a>访问其他应用中的Preference（使用的应用 preference创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE ）</h4><ol>
<li>根据此应用的包名 创建Content</li>
<li>通过创建Content访问preference ，访问preference时会在应用所在包下的shared_prefs目录找到preference</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">Context otherAppsContext = createPackageContext(&quot;cn.kieran.action&quot;, Context.CONTEXT_IGNORE_SECURITY);</div><div class="line">SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences(&quot;testshare&quot;, Context.MODE_WORLD_READABLE);</div><div class="line">String name = sharedPreferences.getString(&quot;name&quot;, &quot;&quot;);</div><div class="line">int age = sharedPreferences.getBoolean(&quot;isboy&quot;, &quot;&quot;);</div></pre></td></tr></table></figure>
<p>如果不通过创建Context访问其他应用的preference，也可以以读取xml文件方式直接访问其他应用preference对应的xml文件，如： </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">File xmlFile = new File(“/data/data/cn.kieran.action/shared_prefs/testshare.xml”);。</div></pre></td></tr></table></figure>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://kieran.cn/2017/06/09/android-入门第一章/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kieran zhi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kieran">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/06/09/android-入门第一章/" itemprop="url">android :AndroidManifest.xml</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-09T16:30:00+08:00">
                2017-06-09
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/android/" itemprop="url" rel="index">
                    <span itemprop="name">android</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          
          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="关于-AndroidManifest-xml"><a href="#关于-AndroidManifest-xml" class="headerlink" title="关于 AndroidManifest.xml"></a>关于 AndroidManifest.xml</h2><p>是安装程序的必须文件，是整个项目的配置文件，对各个节点进行分析</p>
<h4 id="第一层-Manifest-属性"><a href="#第一层-Manifest-属性" class="headerlink" title="第一层 Manifest : (属性)"></a>第一层 Manifest : (属性)</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&lt;manifest  xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</div><div class="line">          package=&quot;com.roobo.test&quot;</div><div class="line">          android:sharedUserId=&quot;string&quot;</div><div class="line">          android:sharedUserLabel=&quot;string resource&quot;</div><div class="line">          android:versionCode=&quot;integer&quot;</div><div class="line">          android:versionName=&quot;string&quot;</div><div class="line">          android:installLocation=[&quot;auto&quot; | &quot;internalOnly&quot; | &quot;preferExternal&quot;] &gt;</div><div class="line">&lt;/manifest&gt;</div></pre></td></tr></table></figure>
<ul>
<li><p>xmlns:android</p>
<blockquote>
<p>定义android命名空间，一般为<br><a href="http://schemas.android.com/apk/res/android，" target="_blank" rel="external">http://schemas.android.com/apk/res/android，</a>  这样使得Android中各种标准属性能在文件中使用，提供了大部分元素中的数据</p>
</blockquote>
</li>
<li><p>package</p>
<blockquote>
<p>指定本应用内java主程序包的包名，它也是一个应用程序的唯一标识符</p>
</blockquote>
</li>
<li><p>sharedUserId</p>
<blockquote>
<p>Android给每个APK进程分配一个单独的用户空间,其manifest中的userid就是对应一个Linux用户<br>(Android 系统是基于Linux)的.<br>所以不同APK(用户)间互相访问数据默认是禁止的.<br>但是它也提供了2种APK间共享数据的形式:<br>相当于ios 的 URL Schemes(应用间跳转)</p>
</blockquote>
</li>
<li><p>sharedUserLabel</p>
<blockquote>
<p>一个共享的用户名，它只有在设置了sharedUserId属性的前提下才会有意义</p>
</blockquote>
</li>
<li><p>versionCode</p>
<blockquote>
<p>是给设备程序识别版本(升级)用的必须是一个interger值代表app更新过多少次，比如第一版一般为1，之后若要更新版本就设置为2，3等等</p>
</blockquote>
</li>
<li><p>versionName</p>
<blockquote>
<p>这个名称是给用户看的,如当前的版本是 2.0.1</p>
</blockquote>
</li>
<li><p>installLocation</p>
<blockquote>
<p>安装参数，是Android2.2中的一个新特性，installLocation有三个值可以选择：internalOnly、auto、preferExternal</p>
</blockquote>
</li>
</ul>
<blockquote>
<p>选择preferExternal,系统会优先考虑将APK安装到SD卡上(当然最终用户可以选择为内部ROM存储上，如果SD存储已满，也会安装到内部存储上)</p>
<p>选择auto，系统将会根据存储空间自己去适应</p>
<p>选择internalOnly是指必须安装到内部才能运行</p>
</blockquote>
<p><code>(注：需要进行后台类监控的APP最好安装在内部，而一些较大的游戏APP最好安装在SD卡上。现默认为安装在内部，如果把APP安装在SD卡上，首先得设置你的level为8，并且要配置android:installLocation这个参数的属性为preferExternal)</code></p>
<h4 id="第二层Application属性"><a href="#第二层Application属性" class="headerlink" title="第二层Application属性"></a>第二层Application属性</h4><h5 id="每个app-都必须声明当前配置标签-标签声明了每一个应用程序的组件及其属性-如icon-label-permission等"><a href="#每个app-都必须声明当前配置标签-标签声明了每一个应用程序的组件及其属性-如icon-label-permission等" class="headerlink" title="每个app 都必须声明当前配置标签,标签声明了每一个应用程序的组件及其属性(如icon,label,permission等)"></a>每个app 都必须声明当前配置标签,标签声明了每一个应用程序的组件及其属性(如icon,label,permission等)</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">&lt;application  android:allowClearUserData=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">             android:allowTaskReparenting=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">             android:backupAgent=&quot;string&quot;</div><div class="line">             android:debuggable=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">             android:description=&quot;string resource&quot;</div><div class="line">             android:enabled=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">             android:hasCode=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">             android:icon=&quot;drawable resource&quot;</div><div class="line">             android:killAfterRestore=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">             android:label=&quot;string resource&quot;</div><div class="line">             android:manageSpaceActivity=&quot;string&quot;</div><div class="line">             android:name=&quot;string&quot;</div><div class="line">             android:permission=&quot;string&quot;</div><div class="line">             android:persistent=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">             android:process=&quot;string&quot;</div><div class="line">             android:restoreAnyVersion=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">             android:taskAffinity=&quot;string&quot;</div><div class="line">             android:theme=&quot;resource or theme&quot; &gt;</div><div class="line">&lt;/application&gt;</div></pre></td></tr></table></figure>
<ul>
<li>android:allowClearUserData(‘true’ or ‘false’)</li>
</ul>
<blockquote>
<p>用户是否能选择自行清除数据，默认为true，程序管理器包含一个选择允许用户清除数据。当为true时，用户可自己清理用户数据，反之亦然</p>
</blockquote>
<ul>
<li>android:allowTaskReparenting(‘true’ or ‘false’)</li>
</ul>
<blockquote>
<p>是否允许activity更换从属的任务，比如从短信息任务切换到浏览器任务</p>
</blockquote>
<ul>
<li>android:backupAgent</li>
</ul>
<blockquote>
<p>设置该APP的备份，属性值应该是一个完整的类名，如com.project.TestCase，此属性并没有默认值，并且类名必须得指定(就是个备份工具，将数据备份到云端的操作)</p>
</blockquote>
<ul>
<li>android:debuggable</li>
</ul>
<blockquote>
<p>这个从字面上就可以看出是什么作用的，当设置为true时，表明该APP在手机上可以被调试。默认为false,在false的情况下调试该APP，就会报以下错误：</p>
<p>Device XXX requires that applications explicitely declare themselves as debuggable in their manifest.</p>
<p> Application XXX does not have the attribute ‘debuggable’ set to TRUE in its manifest and cannot be debugged.</p>
</blockquote>
<ul>
<li>android:description/android:label</li>
</ul>
<blockquote>
<p>此两个属性都是为许可提供的，均为字符串资源，当用户去看许可列表(android:label)或者某个许可的详细信息(android:description)时，这些字符串资源就可以显示给用户。label应当尽量简短，之需要告知用户该许可是在保护什么功能就行。而description可以用于具体描述获取该许可的程序可以做哪些事情，实际上让用户可以知道如果他们同意程序获取该权限的话，该程序可以做什么。我们通常用两句话来描述许可，第一句描述该许可，第二句警告用户如果批准该权限会可能有什么不好的事情发生</p>
</blockquote>
<ul>
<li>android:enabled</li>
</ul>
<blockquote>
<p>Android系统是否能够实例化该应用程序的组件，如果为true，每个组件的enabled属性决定那个组件是否可以被 enabled。如果为false，它覆盖组件指定的值；所有组件都是disabled。</p>
</blockquote>
<ul>
<li>android:hasCode(‘true’ or ‘false’)</li>
</ul>
<blockquote>
<p>表示此APP是否包含任何的代码，默认为true，若为false，则系统在运行组件时，不会去尝试加载任何的APP代码</p>
<p>一个应用程序自身不会含有任何的代码，除非内置组件类，比如Activity类，此类使用了AliasActivity类，当然这是个罕见的现象</p>
<p>(在Android2.3可以用标准C来开发应用程序，可在androidManifest.xml中将此属性设置为false,因为这个APP本身已经不含有任何的JAVA代码了)</p>
</blockquote>
<ul>
<li>android:icon</li>
</ul>
<blockquote>
<p>就是声明整个APP的图标，图片一般都放在drawable文件夹下</p>
</blockquote>
<ul>
<li>android:killAfterRestore</li>
</ul>
<blockquote>
<p>这个属性是指在一个完整的系统恢复操作之后应用程序是否被终止。单个应用程序的恢复操作不会引起应用程序的终止。完整的系统恢复操作一般仅在手机首次安装时才会发生一次。第三方应用通常都不需要使用该属性。</p>
<p>该属性的默认值为true，意为在完整的系统恢复期间，应用程序在结束处理其数据之后将被终止。</p>
</blockquote>
<ul>
<li>android:manageSpaceActivity</li>
</ul>
<blockquote>
<p>意思是用于指定一个Activity来管理数据，加上这个属性，指定SettingActivity为管理空间的Activity</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&lt;application  </div><div class="line">        android:manageSpaceActivity=&quot;.activity.SettingActivity&quot; &gt;  </div><div class="line">&lt;/application&gt;</div></pre></td></tr></table></figure>
<ul>
<li>android:name</li>
</ul>
<blockquote>
<p>为应用程序所实现的Application子类的全名。当应用程序进程开始时，该类在所有应用程序组件之前被实例化。</p>
<p>若该类(比方androidMain类)是在声明的package下，则可以直接声明android:name=”androidMain”,但此类是在package下面的子包的话，就必须声明为全路径或android:name=”package名称.子包名成.androidMain”</p>
</blockquote>
<ul>
<li>android:permission</li>
</ul>
<blockquote>
<p>设置许可名，这个属性若在<application>上定义的话，是一个给应用程序的所有组件设置许可的便捷方式，当然它是被各组件设置的许可名所覆盖的</application></p>
</blockquote>
<ul>
<li>android:presistent</li>
</ul>
<blockquote>
<p>该应用程序是否应该在任何时候都保持运行状态,默认为false。因为应用程序通常不应该设置本标识，持续模式仅仅应该设置给某些系统应用程序才是有意义的。</p>
</blockquote>
<ul>
<li>android:process</li>
</ul>
<blockquote>
<p>应用程序运行的进程名，它的默认值为<manifest>元素里设置的包名，当然每个组件都可以通过设置该属性来覆盖默认值。如果你想两个应用程序共用一个进程的话，你可以设置他们的android:process相同，但前提条件是他们共享一个用户ID及被赋予了相同证书的时候</manifest></p>
</blockquote>
<ul>
<li>android:restoreAnyVersion</li>
</ul>
<blockquote>
<p>用来表明应用是否准备尝试恢复所有的备份，甚至该备份是比当前设备上更要新的版本，默认是false</p>
</blockquote>
<ul>
<li>android:taskAffinity</li>
</ul>
<blockquote>
<p>拥有相同的affinity的Activity理论上属于相同的Task，应用程序默认的affinity的名字是<manifest>元素中设定的package名</manifest></p>
</blockquote>
<ul>
<li>android:theme</li>
</ul>
<blockquote>
<p>是一个资源的风格，它定义了一个默认的主题风格给所有的activity,当然也可以在自己的theme里面去设置它，有点类似style。</p>
</blockquote>
<h4 id="第三层-属性"><a href="#第三层-属性" class="headerlink" title="第三层:属性"></a>第三层<activity>:属性</activity></h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">&lt;activity android:allowTaskReparenting=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">          android:alwaysRetainTaskState=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">          android:clearTaskOnLaunch=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">          android:configChanges=[&quot;mcc&quot;, &quot;mnc&quot;, &quot;locale&quot;,</div><div class="line">                                 &quot;touchscreen&quot;, &quot;keyboard&quot;, &quot;keyboardHidden&quot;,</div><div class="line">                                 &quot;navigation&quot;, &quot;orientation&quot;, &quot;screenLayout&quot;,</div><div class="line">                                 &quot;fontScale&quot;, &quot;uiMode&quot;]</div><div class="line">          android:enabled=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">          android:excludeFromRecents=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">          android:exported=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">          android:finishOnTaskLaunch=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">          android:icon=&quot;drawable resource&quot;</div><div class="line">          android:label=&quot;string resource&quot;</div><div class="line">          android:launchMode=[&quot;multiple&quot; | &quot;singleTop&quot; |</div><div class="line">                              &quot;singleTask&quot; | &quot;singleInstance&quot;]</div><div class="line">          android:multiprocess=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">          android:name=&quot;string&quot;</div><div class="line">          android:noHistory=[&quot;true&quot; | &quot;false&quot;]  </div><div class="line">          android:permission=&quot;string&quot;</div><div class="line">          android:process=&quot;string&quot;</div><div class="line">          android:screenOrientation=[&quot;unspecified&quot; | &quot;user&quot; | &quot;behind&quot; |</div><div class="line">                                     &quot;landscape&quot; | &quot;portrait&quot; |</div><div class="line">                                     &quot;sensor&quot; | &quot;nosensor&quot;]</div><div class="line">          android:stateNotNeeded=[&quot;true&quot; | &quot;false&quot;]</div><div class="line">          android:taskAffinity=&quot;string&quot;</div><div class="line">          android:theme=&quot;resource or theme&quot;</div><div class="line">          android:windowSoftInputMode=[&quot;stateUnspecified&quot;,</div><div class="line">                                       &quot;stateUnchanged&quot;, &quot;stateHidden&quot;,</div><div class="line">                                       &quot;stateAlwaysHidden&quot;, &quot;stateVisible&quot;,</div><div class="line">                                       &quot;stateAlwaysVisible&quot;, &quot;adjustUnspecified&quot;,</div><div class="line">                                       &quot;adjustResize&quot;, &quot;adjustPan&quot;] &gt;   </div><div class="line">&lt;/activity&gt;</div></pre></td></tr></table></figure>
<p>*android:clearTaskOnLaunch </p>
<blockquote>
<p>比如 P 是 activity, Q 是被P 触发的 activity, 然后返回Home, 重新启动 P，是否显示 Q</p>
</blockquote>
<p>*android:excludeFromRecents</p>
<blockquote>
<p>是否可被显示在最近打开的activity列表里，默认是false</p>
</blockquote>
<ul>
<li>android:finishOnTaskLaunch</li>
</ul>
<blockquote>
<p>当用户重新启动这个任务的时候，是否关闭已打开的activity，默认是false</p>
<p>如果这个属性和allowTaskReparenting都是true,这个属性就是王牌。Activity的亲和力将被忽略。该Activity已经被摧毁并非re-parented</p>
</blockquote>
<ul>
<li>android:launchMode(Activity加载模式)</li>
</ul>
<blockquote>
<p>在多Activity开发中，有可能是自己应用之间的Activity跳转，或者夹带其他应用的可复用Activity。可能会希望跳转到原来某个Activity实例，而不是产生大量重复的Activity。这需要为Activity配置特定的加载模式，而不是使用默认的加载模式</p>
<p>Activity有四种加载模式：</p>
<p>standard、singleTop、singleTask、singleInstance(其中前两个是一组、后两个是一组)，默认为standard </p>
<p>standard：就是intent将发送给新的实例，所以每次跳转都会生成新的activity。</p>
<p>singleTop：也是发送新的实例，但不同standard的一点是，在请求的Activity正好位于栈顶时(配置成singleTop的Activity)，不会构造新的实例</p>
<p>singleTask：和后面的singleInstance都只创建一个实例，当intent到来，需要创建设置为singleTask的Activity的时候，系统会检查栈里面是否已经有该Activity的实例。如果有直接将intent发送给它。</p>
<p>singleInstance：</p>
<p>首先说明一下task这个概念，Task可以认为是一个栈，可放入多个Activity。比如启动一个应用，那么Android就创建了一个Task，然后启动这个应用的入口Activity，那在它的界面上调用其他的Activity也只是在这个task里面。那如果在多个task中共享一个Activity的话怎么办呢。举个例来说，如果开启一个导游服务类的应用程序，里面有个Activity是开启GOOGLE地图的，当按下home键退回到主菜单又启动GOOGLE地图的应用时，显示的就是刚才的地图，实际上是同一个Activity，实际上这就引入了singleInstance。singleInstance模式就是将该Activity单独放入一个栈中，这样这个栈中只有这一个Activity，不同应用的intent都由这个Activity接收和展示，这样就做到了共享。当然前提是这些应用都没有被销毁，所以刚才是按下的HOME键，如果按下了返回键，则无效</p>
</blockquote>
<ul>
<li>android:multiprocess</li>
</ul>
<blockquote>
<p>是否允许多进程，默认是false</p>
</blockquote>
<ul>
<li>android:noHistory</li>
</ul>
<blockquote>
<p>当用户从Activity上离开并且它在屏幕上不再可见时，Activity是否从Activity stack中清除并结束。默认是false。Activity不会留下历史痕迹</p>
</blockquote>
<ul>
<li>android:screenOrientation</li>
</ul>
<blockquote>
<p>activity显示的模式</p>
<p>默认为unspecified：由系统自动判断显示方向</p>
<p>landscape横屏模式，宽度比高度大</p>
<p>portrait竖屏模式, 高度比宽度大</p>
<p>user模式，用户当前首选的方向</p>
<p>behind模式：和该Activity下面的那个Activity的方向一致(在Activity堆栈中的)</p>
<p>sensor模式：有物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换<br>nosensor模式：忽略物理感应器，这样就不会随着用户旋转设备而更改了</p>
</blockquote>
<ul>
<li>android:stateNotNeeded</li>
</ul>
<blockquote>
<p>activity被销毁或者成功重启时是否保存状态</p>
</blockquote>
<ul>
<li>android:windowSoftInputMode</li>
</ul>
<blockquote>
<p>activity主窗口与软键盘的交互模式，可以用来避免输入法面板遮挡问题，Android1.5后的一个新特性。</p>
<p>这个属性能影响两件事情：</p>
<p>【A】当有焦点产生时，软键盘是隐藏还是显示</p>
<p>【B】是否减少活动主窗口大小以便腾出空间放软键盘</p>
<p>各值的含义：</p>
<p>【A】stateUnspecified：软键盘的状态并没有指定，系统将选择一个合适的状态或依赖于主题的设置</p>
<p>【B】stateUnchanged：当这个activity出现时，软键盘将一直保持在上一个activity里的状态，无论是隐藏还是显示</p>
<p>【C】stateHidden：用户选择activity时，软键盘总是被隐藏</p>
<p>【D】stateAlwaysHidden：当该Activity主窗口获取焦点时，软键盘也总是被隐藏的</p>
<p>【E】stateVisible：软键盘通常是可见的</p>
<p>【F】stateAlwaysVisible：用户选择activity时，软键盘总是显示的状态</p>
<p>【G】adjustUnspecified：默认设置，通常由系统自行决定是隐藏还是显示</p>
<p>【H】adjustResize：该Activity总是调整屏幕的大小以便留出软键盘的空间</p>
<p>【I】adjustPan：当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分</p>
</blockquote>
<h4 id="第四层"><a href="#第四层" class="headerlink" title="第四层()"></a>第四层(<intent-filter>)</intent-filter></h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;intent-filter  android:icon=&quot;drawable resource&quot;</div><div class="line">               android:label=&quot;string resource&quot;</div><div class="line">               android:priority=&quot;integer&quot; &gt;</div><div class="line">      &lt;action /&gt;</div><div class="line">      &lt;category /&gt;</div><div class="line">      &lt;data /&gt;</div><div class="line">&lt;/intent-filter&gt;</div></pre></td></tr></table></figure>
<ul>
<li>intent-filter属性</li>
</ul>
<blockquote>
<p>android:priority(解释：有序广播主要是按照声明的优先级别，如A的级别高于B，那么，广播先传给A，再传给B。优先级别就是用设置priority属性来确定，范围是从-1000～1000，数越大优先级别越高)</p>
<p>Intent filter内会设定的资料包括action,data与category三种。也就是说filter只会与intent里的这三种资料作对比动作</p>
</blockquote>
<ul>
<li>action属性</li>
</ul>
<blockquote>
<p>action很简单，只有android:name这个属性。常见的android:name值为android.intent.action.MAIN，表明此activity是作为应用程序的入口。有关android:name具体有哪些值，可参照这个网址：<a href="http://hi.baidu.com/linghtway/blog/item/83713cc1c2d053170ff477a7.html" target="_blank" rel="external">http://hi.baidu.com/linghtway/blog/item/83713cc1c2d053170ff477a7.html</a></p>
</blockquote>
<ul>
<li>category属性</li>
</ul>
<blockquote>
<p>category也只有android:name属性。常见的android:name值为android.intent.category.LAUNCHER(决定应用程序是否显示在程序列表里)</p>
</blockquote>
<ul>
<li>data属性</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;data  android:host=&quot;string&quot;</div><div class="line">      android:mimeType=&quot;string&quot;</div><div class="line">      android:path=&quot;string&quot;</div><div class="line">      android:pathPattern=&quot;string&quot;</div><div class="line">      android:pathPrefix=&quot;string&quot;</div><div class="line">      android:port=&quot;string&quot;</div><div class="line">      android:scheme=&quot;string&quot;/&gt;</div></pre></td></tr></table></figure>
<blockquote>
<p>【1】每个<data>元素指定一个URI和数据类型（MIME类型）。它有四个属性scheme、host、port、path对应于URI的每个部分：<br>scheme://host:port/path</data></p>
<p>scheme的值一般为”http”，host为包名，port为端口号，path为具体地址。如：<a href="http://com.test.project:200/folder/etc" target="_blank" rel="external">http://com.test.project:200/folder/etc</a></p>
<p>其中host和port合起来构成URI的凭据(authority)，如果host没有指定，则port也会被忽略</p>
<p>要让authority有意义，scheme也必须要指定。要让path有意义，scheme+authority也必须要指定</p>
<p>【2】mimeType（指定数据类型），若mimeType为’Image’，则会从content Provider的指定地址中获取image类型的数据。还有’video’啥的，若设置为video/mp4，则表示在指定地址中获取mp4格式的video文件</p>
<p>【3】而pathPattern和PathPrefix主要是为了格式化path所使用的</p>
</blockquote>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://kieran.cn/2017/01/18/cocoapod/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kieran zhi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kieran">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/01/18/cocoapod/" itemprop="url">cocoapod 笔记</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-01-18T14:12:00+08:00">
                2017-01-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/IOS/" itemprop="url" rel="index">
                    <span itemprop="name">IOS</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          
          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="设置gem为最新版本"><a href="#设置gem为最新版本" class="headerlink" title="设置gem为最新版本"></a>设置gem为最新版本</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo gem update --system</div></pre></td></tr></table></figure>
<h3 id="pod-安装"><a href="#pod-安装" class="headerlink" title="pod 安装"></a>pod 安装</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">$ gem update --system # 这里请翻墙一下</div><div class="line"></div><div class="line">$ gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/</div><div class="line"></div><div class="line">$ gem sources -l</div><div class="line"></div><div class="line"># https://gems.ruby-china.org</div><div class="line"></div><div class="line">$ sudo gem install cocoapods</div><div class="line"></div><div class="line"> sudo gem install -n /usr/local/bin cocoapods</div><div class="line">解决无安装权限</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">$ pod setup</div><div class="line"></div><div class="line">cd ~/.cocoapods</div><div class="line"></div><div class="line">du -sh *</div></pre></td></tr></table></figure>
<h3 id="cocoapod-私有库创建"><a href="#cocoapod-私有库创建" class="headerlink" title="cocoapod 私有库创建"></a>cocoapod 私有库创建</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pod lib create RBBusKit</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pod lib lint</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">pod trunk push //提交公有</div><div class="line">pod trunk push --allow-warnings</div></pre></td></tr></table></figure>
<hr>
<h3 id="cocoapod-私有库"><a href="#cocoapod-私有库" class="headerlink" title="cocoapod 私有库"></a>cocoapod 私有库</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">#podfile 文件路径</div><div class="line">pod repo add RBVideoSDK  https://git.365jiating.com/zhikuiyu/RBVideoKit.git</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pod repo push RBVideoSDK RBVideoSDK.podspec</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pod lib lint --sources=git@git.365jiating.com:zhikuiyu/videoSpec.git,master --no-clean --verbose --allow-warnings</div></pre></td></tr></table></figure>
<h3 id="搜索不到内容"><a href="#搜索不到内容" class="headerlink" title="搜索不到内容"></a>搜索不到内容</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm ~/Library/Caches/CocoaPods/search_index.json</div></pre></td></tr></table></figure>
<h3 id="pod-注册"><a href="#pod-注册" class="headerlink" title="pod 注册"></a>pod 注册</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">pod trunk register zhikuiyu@roo.bo &apos;zhikuiyu&apos; --description=&apos;zhikuiyu&apos;</div><div class="line"></div><div class="line">pod trunk register zky_416@sina.com &apos;zhikuiyu&apos; --description=&apos;zhikuiyu&apos;</div></pre></td></tr></table></figure>
<h3 id="pod"><a href="#pod" class="headerlink" title="pod"></a>pod</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">- ERROR | [iOS] unknown: Encountered an unknown error (757: unexpected token a</div><div class="line"></div><div class="line">launchctl remove com.apple.CoreSimulator.CoreSimulatorService</div><div class="line">killall -9 com.apple.CoreSimulator.CoreSimulatorService</div></pre></td></tr></table></figure>
          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://kieran.cn/2017/01/18/ios push 证书/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kieran zhi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kieran">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/01/18/ios push 证书/" itemprop="url">ios php push证书</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-01-18T14:12:00+08:00">
                2017-01-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/工作笔记/" itemprop="url" rel="index">
                    <span itemprop="name">工作笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          
          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="IOS-PhP-证书生成"><a href="#IOS-PhP-证书生成" class="headerlink" title="IOS PhP 证书生成"></a>IOS PhP 证书生成</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">openssl pkcs12 -nocerts -out PushChatKey.pem -in apns-dev-cert.p12</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cat PushChatKey.pem apns-dev-cert.pem &gt; ck.pem</div></pre></td></tr></table></figure>
          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://kieran.cn/2017/01/18/文件路径/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kieran zhi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kieran">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/01/18/文件路径/" itemprop="url">文件路径</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-01-18T14:12:00+08:00">
                2017-01-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/IOS/" itemprop="url" rel="index">
                    <span itemprop="name">IOS</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          
          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="组件路径"><a href="#组件路径" class="headerlink" title="组件路径"></a>组件路径</h1><h3 id="RBBusKit"><a href="#RBBusKit" class="headerlink" title="RBBusKit"></a>RBBusKit</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/Users/zky/RBBusKit</div></pre></td></tr></table></figure>
<h3 id="DeviceData-路径"><a href="#DeviceData-路径" class="headerlink" title="DeviceData 路径"></a>DeviceData 路径</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/Users/roobo/Library/Developer/Xcode/DerivedData</div></pre></td></tr></table></figure>
<h3 id="清理编译缓存"><a href="#清理编译缓存" class="headerlink" title="清理编译缓存"></a>清理编译缓存</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">//xcode 缓存地址</div><div class="line">~/Library/Developer/Xcode/Products</div></pre></td></tr></table></figure>
          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://kieran.cn/2017/01/18/解决本地多个ssh key问题/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kieran zhi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Kieran">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2017/01/18/解决本地多个ssh key问题/" itemprop="url">Git 使用笔记</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-01-18T14:12:00+08:00">
                2017-01-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Git/" itemprop="url" rel="index">
                    <span itemprop="name">Git</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          
          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="解决本地多个ssh-key问题"><a href="#解决本地多个ssh-key问题" class="headerlink" title="解决本地多个ssh key问题"></a>解决本地多个ssh key问题</h2><p>有的时候，不仅github使用ssh key，工作项目或者其他云平台可能也需要使用ssh key来认证，如果每次都覆盖了原来的id_rsa文件，那么之前的认证就会失效。这个问题我们可以通过在~/.ssh目录下增加config文件来解决。</p>
<p>下面以配置git oschina的ssh key为例。</p>
<ol>
<li>生成ssh key时同时指定保存的文件名,这里也可以不用指定，只需要在创建的时候根据提示修改就可以，这里使用直接指定路径文件名的方式</li>
</ol>
<p>ssh-keygen -t rsa -f ~/.ssh/oschina/id_rsa -C “email”<br>这时~/.ssh目录下会多出oschina/id_rsa和oschina/id_rsa.pub两个文件，id_rsa.pub里保存的就是我们要使用的key。</p>
<ol>
<li>新增并配置config文件</li>
</ol>
<p>添加config文件</p>
<p>如果config文件不存在，先添加；存在则直接修改</p>
<p>touch ~/.ssh/config<br>在config文件里添加如下内容(User表示你的用户名)</p>
<p>Host git.oschina.net<br>    HostName git.oschina.net<br>    IdentityFile ~/.ssh/oschina/id_rsa<br>    User git</p>
<ol>
<li>上传key到oschina <a href="http://git.oschina.net/profile/sshkeys" target="_blank" rel="external">http://git.oschina.net/profile/sshkeys</a></li>
</ol>
<ol>
<li>测试ssh key是否配置成功</li>
</ol>
<p>ssh -T git@git.oschina.net<br>成功的话会显示：</p>
<p>Welcome to Git@OSC, 张大鹏!<br>至此，本地便成功配置多个ssh key。日后如需添加，则安装上述配置生成key，并修改config文件即可。</p>
<p>ssh-keygen -t rsa -C “注册的github邮箱”<br>终端执行命令：ssh-keygen -t rsa -C “注册的github邮箱”，这次一定要注意，对生成定的秘钥进行重命名，这里暂且重命名为id_rsa_home,同样不设置密码。可以看到生成的公私秘钥已经分别被重命名为id_rsa_home.pub和id_rsa_home。<br>检测</p>
<h2 id="配置config"><a href="#配置config" class="headerlink" title="配置config"></a>配置config</h2><p>在.ssh/目录下新建config文件：touch config，通过nano编辑器进行如下配置：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">Host git.365jiating.com</div><div class="line">    HostName git.365jiating.com</div><div class="line">    IdentityFile ~/.ssh/roobo/id_rsa</div><div class="line">    User git</div></pre></td></tr></table></figure></p>
<p>检测gitlab连接，如果提示是否建立连接，直接yes就行：</p>
<p>检测github连接：</p>
<p>不报错的话，就说明设置成功了！</p>
<h2 id="步骤二：配置-ssh-config文件，以我自己的机器为例。"><a href="#步骤二：配置-ssh-config文件，以我自己的机器为例。" class="headerlink" title="步骤二：配置~/.ssh/config文件，以我自己的机器为例。"></a>步骤二：配置~/.ssh/config文件，以我自己的机器为例。</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">#Default Git</div><div class="line">Host defaultgit</div><div class="line">  HostName IP Address #域名也可</div><div class="line">  User think</div><div class="line">  IdentityFile ~/.ssh/id_rsa</div><div class="line"></div><div class="line">#Second Git</div><div class="line">Host secondgit</div><div class="line">  HostName IP Address #域名也可</div><div class="line">  User think</div><div class="line">  IdentityFile ~/.ssh/id_rsa_second</div></pre></td></tr></table></figure>
<h2 id="步骤三：执行ssh-agent让ssh识别新的私钥。"><a href="#步骤三：执行ssh-agent让ssh识别新的私钥。" class="headerlink" title="步骤三：执行ssh-agent让ssh识别新的私钥。"></a>步骤三：执行ssh-agent让ssh识别新的私钥。</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ssh-add ~/.ssh/id_rsa_new</div></pre></td></tr></table></figure>
<p>该命令如果报错：Could not open a connection to your authentication agent.无法连接到ssh agent，可执行ssh-agent bash命令后再执行ssh-add命令。</p>
<p>也可以修改提交的用户名和Email：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">git commit --amend --author=&apos;Your Name &apos;</div></pre></td></tr></table></figure></p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
  </section>

  


          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="Kieran zhi" />
          <p class="site-author-name" itemprop="name">Kieran zhi</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">4</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/kyios" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              友情链接
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.baidu.com/" title="百度" target="_blank">百度</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://example.com/" title="新浪" target="_blank">新浪</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
<div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Kieran zhi</span>
<span id="busuanzi_container_site_pv">
本站总访问量<span id="busuanzi_value_site_pv"></span>次
<span id="busuanzi_container_site_uv">
本站访客数<span id="busuanzi_value_site_uv"></span>人次
</span>
</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
    <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>

  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>




  


  




	





  
    
    <script>
      var cloudTieConfig = {
        url: document.location.href, 
        sourceId: "",
        productKey: "9da2e20b42e745e2bfe61eb017fc9832",
        target: "cloud-tie-wrapper"
      };
    </script>
    <script src="https://img1.ws.126.net/f2e/tie/yun/sdk/loader.js"></script>
  










  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
